package com.loan.market.util

import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object Retrofit {

    private lateinit var retrofit: Retrofit

    fun getInstance(baseUrl: String): Retrofit {
        if (!this::retrofit.isInitialized) {
            val okHttpClient = OkHttpClient.Builder()
                .dispatcher(Dispatcher().apply { maxRequestsPerHost = 10 })
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build()
            retrofit = Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(GsonUtil.getInstance()))
                .client(okHttpClient)
                .build()
        }
        return retrofit
    }

}